package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.ResourceRoleTypeService;
import org.scboy.rbac.service.RoleTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rbac/roletype/")
@Menu("权限管理系统.角色类型")
public class RoleTypeController {
	
	@Autowired
	RoleTypeService  service;
	

	@Autowired
	ResourceRoleTypeService resourceRoleTypeService;
	
	
	@PostMapping("page")
	@Permission("分页查询角色类型")
	public Result page(@RequestBody Model params) {
		Page page=service.page(params);
		return Result.Success(page);
	}
	
	@GetMapping("list")
	@Permission("获取所有启用的角色类型")
	public Result list(String orgId) {
		return Result.Success(service.list(orgId));
	}

	@PostMapping("add")
	@Permission("新增角色类型")
	public Result add(@RequestBody Model params){
		boolean check=service.add(params);
		if(check) {
			return Result.Success("RBAC_ROLE_TYPE_ADD_SUCCESS");
		}else {
			return Result.Error("RBAC_ROLE_TYPE_ADD_ERROR");
		}
	}
	
	@PutMapping("update")
	@Permission("更新角色类型")
	public Result update(@RequestBody Model params){
		boolean check=service.update(params);
		if(check) {
			return Result.Success("RBAC_ROLE_TYPE_UPDATE_SUCCESS");
		}else {
			return Result.Error("RBAC_ROLE_TYPE_UPDATE_ERROR");
		}
	}
	
	@DeleteMapping("delete")
	@Permission("删除角色类型")
	public Result delete(String code){
		boolean check=service.delete(code);
		if(check) {
			return Result.Success("RBAC_ROLE_TYPE_DELETE_SUCCESS");
		}else {
			return Result.Error("RBAC_ROLE_TYPE_DELETE_ERROR");
		}
	}

	@GetMapping("get")
	@Permission("获取角色类型信息")
	public Result get(String code) {
		return Result.Success(service.get(code), "");
	}

	@PutMapping("sort")
	@Permission("升序降序")
	public Result sort(String code,int type) {
		boolean check=service.sort(code,type);
		if(check) {
			return Result.Success("RBAC_ROLE_TYPE_SORT_SUCCESS");
		}else {
			return Result.Error("RBAC_ROLE_TYPE_SORT_ERROR");
		}
	}
	
	
	/**
	 * 分页查询角色类型和角色关系列表
	 * @param request
	 * @return
	 */
	@PostMapping("rutpage")
	@Permission("分页查询角色类型和资源关系列表")
	public Result resourceRoleTypePage(@RequestBody Model params) {
		Page page = resourceRoleTypeService.page(params);
		return Result.Success(page);
	}
	
	/**
	 * 新增角色类型和角色关系
	 * @param request
	 * @param JOBTYPEId
	 * @param roleId
	 * @return
	 */
	@PostMapping("addrut")
	@Permission("新增角色类型和资源关系")
	public Result addResourceRoleType(String code,String[] resId) {
		if (resourceRoleTypeService.addResource(code, resId)) {
			return Result.Success("RBAC_RESOURCE_ROLETYPE_ADD_SUCCESS");
		} else {
			return Result.Error("RBAC_RESOURCE_ROLETYPE_ADD_ERROR");
		}
	}

	/**
	 * 删除角色类型和角色关系
	 * @param request
	 * @param id
	 * @return
	 */
	@DeleteMapping("deleterut")
	@Permission("删除角色类型和资源关系")
	public Result deleteResourceRoleType(String id) {
		if (resourceRoleTypeService.delete(id)) {
			return Result.Success("RBAC_RESOURCE_ROLETYPE_DELETE_SUCCESS");
		} else {
			return Result.Error("RBAC_RESOURCE_ROLETYPE_DELETE_ERROR");
		}
	}
	
	/**
	 * 获取角色类型相关资源ID
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("getrut")
	@Permission("获取角色类型相关资源ID")
	public Result getResourceRoleType(String code) {
		Model model=Model.create().add("resId", resourceRoleTypeService.getResIds(code)).add("code", code);
		return Result.Success(model,"");
	}
}
